<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Repos\ItemRepo;
use App\Models\Box;

class  LogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = [];
        if($request->keyword) {
            $where['keyword'] = trim($request->keyword);
        }
        $list = LogRepo::pages($where);
        return view('hrs.log.index',compact('where','list'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       // $boxes = Box::get();
        return view('hrs.item.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rs['status'] = 'danger';
        $rs['msg'] = '操作失败';
        $data = $request->except(['_token']);
        
        $flag = ItemRepo::save($data);
        if($flag['status']==true){
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return redirect('items/')->with('rs',$rs);
        } 
        $rs['msg'] = $flag['msg'];
        return back()->with('rs',$rs)->withInput();


    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = ItemRepo::find(['id'=>$id]);
        return view('hrs.item.show',compact('data'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $boxes = Box::get();
        $data = ItemRepo::find(['id'=>$id]);
        return view('hrs.item.edit',compact('data','boxes'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $rs['status'] = 'warning';
        $rs['msg']    = '操作失败';
        $data = $request->except(['_token','_method']);
        $flag = ItemRepo::update($data,['id'=>$id]);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return back()->with('rs',$rs);
        }
        return back()->with('rs',$rs);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs['status'] = 'warning';
        $rs['msg']    = '操作失败';
        $flag = ItemRepo::delete($id);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return back()->with('rs',$rs);
        }
        return back()->with('rs',$rs);
    }

    
}
